package com.hyjx.framework.component.codetable;

import com.hyjx.framework.common.tree.TreeObject;
import com.hyjx.framework.common.tree.TreeUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

/**
 * 处理码表数据请求的servlet类【因已实现controller方式，后面将陆续废除此类】
 */
public class SelectServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * 根据请求参数，返回码表数据
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        String codetable = req.getParameter("codetable");
        String parentcode = req.getParameter("parentcode");
        String showtype = req.getParameter("showtype");
        if (showtype == null) {
            deal(codetable, parentcode, resp);
        } else {
            dealComboTree(codetable, parentcode, resp);
        }
    }

    /**
     * 根据请求参数，返回码表数据
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        doGet(req, resp);
    }

    /**
     * 根据请求参数，返回码表数据
     *
     * @param codetable
     * @param parentcode
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    private void dealComboTree(String codetable, String parentcode, HttpServletResponse resp) throws ServletException, IOException {
        List<CodeBean> list;
        if (parentcode != null && !parentcode.equals("")) {
            list = CodeTableManager.getSubCodeList(codetable, parentcode);
        } else {
            list = CodeTableManager.getTableList(codetable);
        }
        List<TreeObject> toList = new ArrayList<TreeObject>();
        for (CodeBean cb : list) {
            TreeObject to = new TreeObject();
            to.setId(cb.getCode());
            to.setParent_id(cb.getParentCode());
            to.setText(cb.getName());
            toList.add(to);
        }
        String sb = TreeUtil.getEasyuiJsonStr(toList, "0", null);
        resp.setContentType("text/plain;charset=UTF-8");
        PrintWriter pw = resp.getWriter();
        pw.write(sb);
        pw.flush();
    }

    /**
     * 根据请求参数，返回码表数据
     *
     * @param codetable
     * @param parentcode
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    private void deal(String codetable, String parentcode, HttpServletResponse resp)
            throws ServletException, IOException {
        List<CodeBean> list;
        if (parentcode != null && !parentcode.equals("")) {
            list = CodeTableManager.getSubCodeList(codetable, parentcode);
        } else {
            list = CodeTableManager.getTableList(codetable);
        }
        StringBuffer sb = new StringBuffer();
        sb.append("{\"result\":[");
        int i = 1;
        if (list != null)
            for (CodeBean bean : list) {
                sb.append("{");
                sb.append("\"code\":\"" + bean.getCode() + "\",");
                sb.append("\"name\":\"" + bean.getName() + "\"");
                sb.append("}");
                if (i != list.size()) {
                    sb.append(",");
                }
                i++;
            }
        sb.append("]}");
        resp.setContentType("text/plain;charset=UTF-8");
        PrintWriter pw = resp.getWriter();
        pw.write(sb.toString());
        pw.flush();
    }
}
